package com.word.app.controller;


import java.lang.reflect.Field;
import java.util.List;

import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.word.app.controller.response.BaseResponse;
import com.word.app.pojo.Subject;
import com.word.app.pojo.dto.PagingReq;
import com.word.app.service.ISubjectService;

/**
 * @author nano
 */
@RestController
@RequestMapping("/subject")
public class SubjectController {

    @Autowired
    ISubjectService service;


    @RequiresPermissions(value={"perm:root", "perm:visitor"}, logical = Logical.OR)
    @PostMapping("/query")
    public BaseResponse query(@RequestBody PagingReq<Subject> req) {
        QueryWrapper<Subject> wrapper = new QueryWrapper<Subject>();
        Subject subject = req.getParams();
        if(subject!=null && subject.getName()!=null){
            wrapper.like("name", String.format("%%%s%%", subject.getName()));
            subject.setName(null);
        }
        wrapper.setEntity(subject);
        return new BaseResponse().data(service.page(req.getPage(), wrapper));
    }
    
    @RequiresPermissions(value="perm:root")
    @PostMapping("/add")
    public void add(@RequestBody Subject subject){
        service.save(subject);
    }

    @RequiresPermissions(value="perm:root")
    @PostMapping("/delete")
    public void delete(@RequestBody List<String> ids){
        service.removeByIds(ids);
    }

    @RequiresPermissions(value="perm:root")
    @PostMapping("/update")
    public void update(@RequestBody Subject subject) {
        service.updateById(subject);
    }
}
